package demo6;

import java.util.Arrays;

public class Test {

    //选择排序
    public void selectSort(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            int min = i;
            for(int j = i + 1; j < arr.length; j++) {
                if(arr[min] > arr[j]) {
                    min = j;
                }
            }
            swap(arr, min, i);
        }
    }

    private void swap(int[] arr, int min, int i) {
        int tmp = arr[min];
        arr[min] = arr[i];
        arr[i] = tmp;
    }

    //堆排序
    public void heapSort(int[] arr) {
        //首先要创建一个大根堆
        createBigHeap(arr);
        int end = arr.length - 1;
        while(end > 0) {
            swap(arr, 0, end);
            adjustDown(arr, 0, end);
            end--;
        }
    }

    private void createBigHeap(int[] arr) {
        int len = arr.length;
        for(int parent = (len - 1 - 1) / 2; parent >= 0; parent--) {
            //对每一颗子树向下调整
            adjustDown(arr, parent, len);
        }
    }

    private void adjustDown(int[] arr, int parent, int len) {
        int child = parent * 2 + 1;
        while(child < len) {
            if(child + 1 < len && arr[child + 1] > arr[child]) {
                child++;
            }
            if(arr[child] > arr[parent]) {
                swap(arr, child, parent);
                //继续向下调整
                parent = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }

    //快速排序
    public void quickSort(int[] arr) {
        quick(arr, 0, arr.length - 1);
    }

    private void quick(int[] arr, int left, int right) {
        if(left >= right) {
            return;
        }
        //三数取中
        int midIndex = fundMidIndex(arr, left, right);
        swap(arr, left, midIndex);
        //寻找基准
        int point = fundPoint(arr, left, right);
        quick(arr, left, point - 1);
        quick(arr, point + 1, right);
    }

    private int fundMidIndex(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        if(arr[left] < arr[right]) {
            if(arr[left] > arr[mid]) {
                return left;
            } else if(arr[right] < arr[mid]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if(arr[mid] > arr[left]) {
                return left;
            } else if(arr[mid] < arr[right]) {
                return right;
            } else {
                return mid;
            }
        }
    }

    private int fundPoint(int[] arr, int start, int end) {
        int key = arr[start];
        while(start < end) {
            while(start < end && key <= arr[end]) {
                end--;
            }
            arr[start] = arr[end];
            while(start < end && arr[start] <= key) {
                start++;
            }
            arr[end] = arr[start];
        }
        arr[start] = key;
        return start;
    }

    //归并排序
    public void mergeSort(int[] arr) {
        mergeDfs(arr, 0, arr.length - 1);
    }

    private void mergeDfs(int[] arr, int left, int right) {
        if(left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeDfs(arr, left, mid);
        mergeDfs(arr, mid + 1, right);
        merge(arr, left, right, mid);
    }

    private void merge(int[] arr, int start, int end, int mid) {
        int[] tmpArr = new int[end - start + 1];
        int index = 0;//tmpArr的下标
        int s1 = start;
        int s2 = mid + 1;
        while(s1 <= mid && s2 <= end) {
            if(arr[s1] < arr[s2]) {
                tmpArr[index++] = arr[s1++];
            } else {
                tmpArr[index++] = arr[s2++];
            }
        }
        //剩下没合并的一起
        while(s1 <= mid) {
            tmpArr[index++] = arr[s1++];
        }
        while(s2 <= end) {
            tmpArr[index++] = arr[s2++];
        }
        //合并
        for(int i = 0; i < index; i++) {
            arr[i + start] = tmpArr[i];
        }
    }

    //计数排序
    public void countSort(int[] arr) {
        int max = arr[0];
        int min = arr[0];
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] > max) {
                max = arr[i];
            }
            if(arr[i] < min) {
                min = arr[i];
            }
        }
        int[] count = new int[max - min + 1];
        //开始计数
        for(int i = 0; i < arr.length; i++) {
            count[arr[i] - min]++;
        }
        //覆盖
        int index = 0;//arr的下标
        for(int i = 0; i < count.length; i++) {
            while(count[i] > 0) {
                arr[index] = i + min;
                count[i]--;
                index++;
            }
        }
    }

    //希尔排序
    public void shellSort(int[] arr) {
        int gap = arr.length;
        while(gap > 1) {
            gap >>= 1;
            shell(arr, gap);
        }
        shell(arr, 1);
    }

    private void shell(int[] arr, int gap) {
        for(int i = 1; i < arr.length; i++) {
            int key = arr[i];
            int j = i - 1;
            for(; j >= 0; j--) {
                if(arr[j] > key) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = key;
        }
    }
}

class Main {
    public static void main(String[] args) {
        Test t = new Test();
        int[] arr = {4, 1, 6, 2, 7, 8, 3, 0, 9, 5};
        t.shellSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}